#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
int N, D, MAX_X;
int Y[100010];
deque<int> Max, Min;
bool check(int W);
int main()
{
	cin >> N >> D;
	fill(Y, Y + 100003, -1);
	for (int i = 0; i<N; i++)
	{
		int x, y;
		cin >> x >> y;
		Y[x] = y;
		MAX_X = max(x, MAX_X);
	}
	int left = 0, right = MAX_X + 1;
	while (left<right - 1)				//×ó¿ªÓÒ±Õ¡£ 
	{
		int mid = (left + right) / 2;
		cout << left << " " << right << " " << mid << " " << check(mid) << endl << endl;
		if (check(mid))	right = mid;
		else			left = mid;
	}
	if (right == MAX_X + 1)	cout << -1;
	else 			cout << right;
}
bool check(int W)
{
	Max.clear();
	Min.clear();
	for (int i = 1; i<=MAX_X; i++)
	{
		if (i >= W)
		{
			if (Y[i - W] == Max.front())	Max.pop_front();
			if (Y[i - W] == Min.front())	Min.pop_front();
		}
		if (Y[i] != -1)
		{
			while (!Max.empty() && Max.back()<Y[i])	Max.pop_back();
			Max.push_back(Y[i]);
			while (!Min.empty() && Min.back()>Y[i])	Min.pop_back();
			Min.push_back(Y[i]);
			//cout<<Max.front()-Min.front()<<endl; 
			if (Max.front() - Min.front() >= D)	return true;
		}
		
	}
	return false;
}